/* Init */
color = {}

/* Globals */
color.DEFAULT = Color( 0, 0, 0, 255 )

/* Check if color is valid */
color.IsValid = function( col )
	return col and col.r and col.g and col.b and col.a
end

/* Get color from table */
color.FromTable = function( tab )

	/* Invalid table */
	if not tab or not tab[1] or not tab[2] or not tab[3] or not tab[4] then
		DEBUG:Msg( "Invalid table for color cast: " .. table.ToString( tab ), "color.FromTable" )
		return color.DEFAULT
	end
	
	/* Try to cast the elements */
	local red		= tonumber( tab[4] )
	local green		= tonumber( tab[3] )
	local blue		= tonumber( tab[2] )
	local alpha		= tonumber( tab[1] )
	
	/* Check values */
	if not red or not green or not blue or not alpha then
		DEBUG:ErrMsg( "Invalid table contents for color cast: " .. table.ToString( tab ), "color.FromTable" )
	end
	
	/* Convert to a color */
	return Color( red, green, blue, alpha )
end

/* Convert color to table */
color.ToTable = function( color )
	
	/* Check type */
	if not IsTable( color ) then return nil end
	
	/* Check color */
	if not IsColor( color ) then return color end
	
	/* Convert to table */
	return
	{
		[1]		= color.a,
		[2]		= color.b,
		[3]		= color.g,
		[4]		= color.r
	}
end

/* Get color from hex string */
color.FromHexString = function( str )

	/* Variables */
	local col		= color.DEFAULT
	
	/* Must start with 0x */
	if str:sub( 1, 2 ) != "0x" then
		DEBUG:ErrMsg( "Invalid hex string: '" .. str .. "'. Must start with '0x'", "color.FromHexString" )
		return col
	end
	
	/* 3 digits */
	if str:len() == 5 then
		col.r		= tonumber( str:rep( str:sub( 3, 3 ), 2 ), 16 )
		col.g		= tonumber( str:rep( str:sub( 4, 4 ), 2 ), 16 )
		col.b		= tonumber( str:rep( str:sub( 5, 5 ), 2 ), 16 )
	
	/* 4 digits */
	elseif str:len() == 6 then
		col.a		= tonumber( str:rep( str:sub( 3, 3 ), 2 ), 16 )
		col.r		= tonumber( str:rep( str:sub( 4, 4 ), 2 ), 16 )
		col.g		= tonumber( str:rep( str:sub( 5, 5 ), 2 ), 16 )
		col.b		= tonumber( str:rep( str:sub( 6, 6 ), 2 ), 16 )
	
	/* 6 digits */
	elseif str:len() == 8 then
		col.r		= tonumber( str:sub( 3, 4 ), 16 )
		col.g		= tonumber( str:sub( 5, 6 ), 16 )
		col.b		= tonumber( str:sub( 7, 8 ), 16 )
	
	/* 8 digits */
	elseif str:len() == 10 then
		col.a		= tonumber( str:sub( 3, 4 ), 16 )
		col.r		= tonumber( str:sub( 5, 6 ), 16 )
		col.g		= tonumber( str:sub( 7, 8 ), 16 )
		col.b		= tonumber( str:sub( 9, 10 ), 16 )
		
	/* Invalid hex color */
	else
		DEBUG:ErrMsg( "Invalid hex color: '" .. str .. "'", "color.FromHexString" )
	end
	
	return col
	
end

/* Converts a color to a hexadecimal string */
color.ToHexString	= function( color )

	return "0x" ..
		string.format( "%02x", color.a ) ..
		string.format( "%02x", color.r ) ..
		string.format( "%02x", color.g ) ..
		string.format( "%02x", color.b )

end

/* Get color from table string */
color.FromString = function( str )
	return color.FromTable( table.FromString( str ) )
end

/* Get string from color */
color.ToString = function( col )
	if not col or not type(col) == "table" then return false end
	return table.ToString( col )
end